<!--  -->
<script setup>
import { ref } from "vue";
import { NCarousel, NCarouselItem, NSpin } from "naive-ui";
import { findRelevantGoods } from "@/api/product";

const props = defineProps({
  goodsId: {
    type: String,
    default: undefined,
  },
});

let show = ref(true);

const goodsList = ref([]);
findRelevantGoods({ id: props.goodsId }).then((res) => {
  const pageSize = 4;
  const count = Math.ceil(res.length / pageSize);
  for (let index = 0; index < count; index++) {
    goodsList.value.push(res.slice(pageSize * index, pageSize * (index + 1)));
  }
  show.value = false;
});
</script>

<template>
  <div class="goods-relevant">
    <div class="header">
      <i class="icon" />
      <span class="title">{{ goodsId ? "同类商品推荐" : "猜你喜欢" }}</span>
    </div>
    <n-spin :show="show">
      <div style="padding: 0px 20px">
        <n-carousel
          autoplay
          slides-per-view="auto"
          :space-between="20"
          :loop="false"
          draggable
          style="height: 330px"
        >
          <n-carousel-item
            v-for="(item, i) in goodsList"
            :key="i"
            class="slider"
          >
            <router-link
              :to="`/product/${goods.id}`"
              v-for="goods in item"
              :key="goods.id"
            >
              <img :src="goods.picture" alt="" class="carousel-img" />
              <p class="name ellipse">{{ goods.name }}</p>
              <p class="price">&yen;{{ goods.price }}</p>
            </router-link>
          </n-carousel-item>
          <template #dots="{ total, currentIndex, to }">
            <ul class="custom-dots">
              <li
                v-for="index of total"
                :key="index"
                :class="{ ['is-active']: currentIndex === index - 1 }"
                @click="to(index - 1)"
              />
            </ul>
          </template>
        </n-carousel>
      </div>
    </n-spin>
  </div>
</template>

<style lang='less' scoped>
// 轮播商品
.slider {
  display: flex;
  justify-content: space-around;
  padding: 0 40px;
  > a {
    width: 240px;
    height: 300px;
    text-align: center;
    img {
      padding: 20px;
      width: 230px !important;
      height: 230px !important;
    }
    .name {
      font-size: 15px;
      color: #666;
      padding: 0 40px;
    }
    .price {
      font-size: 15px;
      color: @priceColor;
      margin-top: 10px;
    }
  }
}
.custom-dots {
  display: flex;
  margin: 0;
  padding: 0;
  position: absolute;
  bottom: 0px;
  z-index: 1;
  left: 50%;
  transform: translate(-50%, 0%);
}

.custom-dots li {
  display: inline-block;
  width: 12px;
  height: 4px;
  margin: 0 3px;
  border-radius: 4px;
  background-color: rgb(194, 194, 194);
  transition: width 0.3s, background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
}

.custom-dots li.is-active {
  width: 40px;
  background: @xtxColor;
}
.carousel-img {
  width: 100%;
  height: 240px;
  object-fit: cover;
}
.goods-relevant {
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
  .header {
    height: 80px;
    line-height: 80px;
    padding: 0 20px;
    .title {
      font-size: 20px;
      padding-left: 10px;
    }
    .icon {
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before {
        content: "";
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background: lighten(@xtxColor, 40%);
      }
    }
  }
}
</style>